/*
 * Copyright (c) 1998-2014 NetSuite, Inc.
 * 2955 Campus Drive, Suite 100, San Mateo, CA, USA 94403-2511
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * NetSuite, Inc. ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with NetSuite.
 */

/*
 * This file allows one to log into NetSuite and execute a variety of web service
 * operations. The only operation that exists initially is a simple get request.
 */
package com.netsuite.dao;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

import javax.xml.rpc.ServiceException;
import javax.xml.soap.SOAPException;

import org.apache.axis.message.SOAPHeaderElement;

import com.netsuite.webservices.platform.core_2014_1.Passport;
import com.netsuite.webservices.platform.core_2014_1.RecordRef;
import com.netsuite.webservices.platform_2014_1.NetSuiteBindingStub;
import com.netsuite.webservices.platform_2014_1.NetSuitePortType;
import com.netsuite.webservices.platform_2014_1.NetSuiteServiceLocator;

/**
 * This is the main class for the SuiteTalk course. All of the web service
 * operations are to be added in this class.
 */
public class MainDAO {

	/*
	 * Proxy object that abstracts the communication with the NetSuite Web
	 * Services. All NetSuite operations are invoked as methods of this class.
	 */
	private NetSuitePortType _service;

	/**
	 * Constuctor. Sets up the NetSuite service
	 * 
	 * @throws ServiceException
	 * @throws FileNotFoundException
	 * @throws IOException
	 * @throws SOAPException
	 */
	public MainDAO() throws ServiceException, FileNotFoundException,
			IOException, SOAPException {

		// In order to use SSL forwarding for SOAP messages. Refer to FAQ for
		// details
		System.setProperty("axis.socketSecureFactory",
				"org.apache.axis.components.net.SunFakeTrustSocketFactory");

		// Locate the NetSuite web service.
		NetSuiteServiceLocator serviceLocator = new NetSuiteServiceLocator();

		// Get the service port
		_service = serviceLocator.getNetSuitePort();

		// Setting client timeout to 2 hours for long running operations
		((NetSuiteBindingStub) _service).setTimeout(1000 * 60 * 60 * 2);

		setPassport(); // set request-level login in the SOAP header
	}

	/**
	 * Sets the authentication information used by each request
	 * 
	 * @throws SOAPException
	 */
	private void setPassport() throws SOAPException {
		// set information into the passport
		Passport passport = new Passport();
		RecordRef roleRef = new RecordRef();

		// replace text in the next 4 lines as noted
		passport.setEmail("mjpascual@netsuite.com");
		passport.setPassword("Keanureeves1");
		roleRef.setInternalId("3");
		passport.setAccount("TSTDRV1196798");

		passport.setRole(roleRef);

		includeInHeader("passport", passport, _service);
	}

	/**
	 * Adds header objects to the request. This is used to set the web service
	 * and search preference from the code. If the specified head already
	 * exists, the value is replaced with the new entry. Otherwise, it's added
	 * to the request.
	 * 
	 * @param headerName
	 *            Either "preference" or "searchpreference".
	 * @param headerValue
	 *            Preference or SearchPreference object depending on what you're
	 *            trying to add.
	 * @param service
	 *            Service that's used in the request.
	 * 
	 * @throws SOAPException
	 */
	public static void includeInHeader(String headerName, Object headerValue,
			NetSuitePortType service) throws SOAPException {
		String namespace = "urn:messages.platform.webservices.netsuite.com";

		// Cast your login NetSuitePortType variable to a NetSuiteBindingStub
		NetSuiteBindingStub binding = (NetSuiteBindingStub) service;

		// Get the current list of headers
		ArrayList<SOAPHeaderElement> headerList = new ArrayList<SOAPHeaderElement>(
				Arrays.asList(binding.getHeaders()));

		// Clear the headers as they do not overwrite when you are using
		// Axis/Java
		binding.clearHeaders();

		// Remove the header that you're adding if it already exists.
		Iterator<SOAPHeaderElement> itr = headerList.iterator();
		while (itr.hasNext()) {
			SOAPHeaderElement header = (SOAPHeaderElement) itr.next();

			if (headerName == header.getName()) {
				itr.remove();
				break;
			}
		}

		// Put the headers back to the service
		for (SOAPHeaderElement header : headerList) {
			binding.setHeader(header);
		}

		// Add the current header to the service.
		binding.setHeader(namespace, headerName, headerValue);
	}

	public NetSuitePortType get_service() {
		return _service;
	}

	
}